package com.personal.website.lottery.spider;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Calendar;

/**
 * Created by 10171225 on 2016/2/17.
 */
public class Util {
    public static long nextTime(SpiderProperties prop){
        long waiting = 0;
        if(prop.getInterval() != null){
            waiting = Integer.parseInt(prop.getInterval()) * 60 * 1000;
        }else{
            String dailyStartTime = prop.getDailyStartTime();
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_YEAR,1);
            calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(dailyStartTime.split(":")[0]));
            if(dailyStartTime.split(":").length == 2){
                calendar.set(Calendar.MINUTE,Integer.parseInt(dailyStartTime.split(":")[1]));
            }
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MILLISECOND,0);
            Calendar currentCalendar =  Calendar.getInstance();
            currentCalendar.set(Calendar.SECOND,0);
            currentCalendar.set(Calendar.MILLISECOND,0);
            waiting = (calendar.getTimeInMillis() - currentCalendar.getTimeInMillis()) % (24 * 3600 * 1000);
        }
        return waiting;
    }

    public static String spideHtml(SpiderProperties spiderProperties){
        String url= spiderProperties.getHostUrl();

        WebClient webClient = null;
        try {
            if(InetAddress.getLocalHost().getCanonicalHostName().trim().endsWith("zte.intra")){
                webClient = new WebClient(BrowserVersion.FIREFOX_24,"proxynj.zte.com.cn",80);
                DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) webClient.getCredentialsProvider();
                credentialsProvider.addCredentials("10171225","Zyf--225");
            } else {
                webClient = new WebClient(BrowserVersion.FIREFOX_24);
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        //设置webClient的相关参数
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(true);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        webClient.getOptions().setTimeout(50000);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        //模拟浏览器打开一个目标网址
        HtmlPage rootPage = null;
        try {
            rootPage = webClient.getPage(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        webClient.closeAllWindows();
        return rootPage.asXml();
    }
}
